/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.common.controller;

import com.koron.workflow.common.job.SynchronizationSysInfoJob;
import com.koron.workflow.common.util.BusinessCode;
import io.swagger.annotations.ApiOperation;
import org.quartz.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.swan.bean.MessageBean;


/**
 * @author qingfeng
 * @description: TODO
 * @date 2020/4/20 18:26
 */
@Controller
public class SystemInfoController  implements InitializingBean {
    // 是否开启同步基础信息
    @Value("${enabledSyncBasicInfo}")
    private String enabledSyncBasicInfo;
    // 开启同步:定时执行时间
    @Value("${syncRunTimeCron}")
    private String syncRunTimeCron;

    // 注入定时任务 scheduler
    @Autowired
    private Scheduler scheduler;

    // 定义 定时任务 key
    private final String JOB_KEY = "synchronizationSysInfo";
    // 定义定时任务 分组名称
    private final String JOB_GROUP = "synchronizationSysInfoGroup";

    @GetMapping("/getEnabledSyncBasicInfo.htm")
    @ApiOperation("获取是否开启同步基础信息")
    public MessageBean getEnabledSyncBasicInfo() {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), boolean.class);
        msg.setData(enabledSyncBasicInfo);
        return msg;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            if ("true".equals(enabledSyncBasicInfo)) {
                /*****************构建定时任务的trigger********************/
                CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(syncRunTimeCron);
                // 创建 trigger
                Trigger trigger = TriggerBuilder.newTrigger()
                        .withIdentity(JOB_KEY, JOB_GROUP)
                        .withSchedule(scheduleBuilder)
                        .startNow()
                        .build();
                JobKey key = JobKey.jobKey(JOB_KEY, JOB_GROUP);
                boolean isExists = scheduler.checkExists(key);
                // 检查 定时任务是存在该 同步基础信息 的 jobkey
                if (!isExists) {// 如果不存在，则新建
                    JobDetail job = JobBuilder.newJob(SynchronizationSysInfoJob.class)
                            .withIdentity(JOB_KEY, JOB_GROUP)
                            .build();
                    scheduler.scheduleJob(job, trigger);
                }
                // 创建 trigger
                trigger = TriggerBuilder.newTrigger()
                        .withIdentity(JOB_KEY, JOB_GROUP + "_Simple")
                        .startNow()
                        .build();
                key = JobKey.jobKey(JOB_KEY, JOB_GROUP + "_Simple");
                isExists = scheduler.checkExists(key);
                // 检查 定时任务是存在该 同步基础信息 的 jobkey
                if (!isExists) {// 如果不存在，则新建
                    JobDetail job = JobBuilder.newJob(SynchronizationSysInfoJob.class)
                            .withIdentity(JOB_KEY, JOB_GROUP + "_Simple")
                            .build();
                    scheduler.scheduleJob(job, trigger);
                }
            } else if (scheduler.isShutdown()) {
                scheduler.shutdown();
            }
            // 启动任务
            if (!scheduler.isShutdown()) {
                scheduler.start();
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
